package dao.Supper.impl;

import dao.BaseDao;
import dao.Supper.SuperGoodsDao;
import pojo.All;
import pojo.Goods;
import pojo.Seller;
import pojo.Smallclass;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 前端商品实现类
 */
public class SuperGoodsImpl extends BaseDao implements SuperGoodsDao {
    /**
     * 得到总数据行
     *
     * @return
     */
    public int getTotalCount(Long id,String smallName,String goodsName ) {
        int res = 0;
        try {
            StringBuffer sql = new StringBuffer("SELECT COUNT(*) FROM  `goods` AS g  \n" +
                    "                   INNER JOIN `seller` AS s ON  g.`goodsSeId` = s.`id`\n" +
                    "                    INNER JOIN `smallclass` AS sm ON g.`goodsSmalId` = sm.`id` where 1=1");
            if (id > 0) {
                sql.append(" and sm.`smallBigId` =" + id);
            }
            if(smallName!=null && smallName!=""){
                sql.append(" and sm.`smallName` =" + "\'" + smallName + "\'");
            }
            if(goodsName !=null && goodsName!=""){
                sql.append(" and g.goodsName like" + "\'%" + goodsName + "%\'");
            }
            ResultSet rs = this.executeSelect(sql.toString(), null);

            if (rs != null) {
                while (rs.next()) {
                    res = rs.getInt(1);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return res;
    }

    /**
     * 显示数据
     *
     * @param currentPage
     * @param pageSize
     * @param id
     * @return
     */
    public List<All> selGoods(int currentPage, int pageSize, Long id,String smallName,String goodsName ) {
        List<All> list = new ArrayList<>();
        try {
            StringBuffer sql = new StringBuffer(" SELECT g.`id` AS ids,`goodsImage`,`goodsName`,`sellerName`,`goodsMoney`,`goodsNumber`,`smallBigId`,smallName FROM  `goods` AS g  \n" +
                    "                    INNER JOIN `seller` AS s ON  g.`goodsSeId` = s.`id`\n" +
                    "                    INNER JOIN `smallclass` AS sm ON g.`goodsSmalId` = sm.`id`  where 1=1");
            if (id > 0) {
                sql.append("  and  sm.`smallBigId` = " + id);
            }
            if(smallName!=null && smallName!=""){
                sql.append(" and sm.`smallName` =" + "\'" + smallName + "\'");
            }
            if(goodsName !=null && goodsName!=""){
                sql.append(" and g.goodsName like" + "\'%" + goodsName + "%\'");
            }
            sql.append("  limit ?,?");
            Object[] params = {(currentPage-1)*pageSize,pageSize};
            ResultSet rs = this.executeSelect(sql.toString(), params);
            while (rs.next()) {
                All all = new All();
                Goods goods = new Goods();
                goods.setId(rs.getLong("ids"));
                goods.setGoodsImage(rs.getString("goodsImage"));
                goods.setGoodsMoney(rs.getDouble("goodsMoney"));
                goods.setGoodsNumber(rs.getLong("goodsNumber"));
                goods.setGoodsName(rs.getString("goodsName"));
                all.setGoods(goods);
                Seller seller = new Seller();
                seller.setSellerName(rs.getString("sellerName"));
                all.setSeller(seller);
                Smallclass smallclass = new Smallclass();
                smallclass.setSmallBigId(rs.getLong("smallBigId"));
                smallclass.setSmallName(rs.getString("smallName"));
                all.setSmallclass(smallclass);
                list.add(all);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return list;
    }

    /**
     * 根据商品id进行查询
     *
     * @param id
     * @return
     */
    public All SelById(Long id) {
        All all = new All();
        try {
            String sql = "SELECT g.`id` as ids,`goodsImage`,`goodsName`,`goodsCarriage`,sm.`smallName`,`goodsNumber`,goodsMoney FROM  `goods` AS g  INNER JOIN `seller` AS s ON  g.`goodsSeId` = s.`id`\n" +
                    "                   inner join `smallclass` AS sm ON g.`goodsSmalId` = sm.`id`\n" +
                    "                   WHERE g.`id` =? ";
            Object[] params = {id};
            ResultSet rs = this.executeSelect(sql, params);
            while (rs.next()) {
                Goods goods = new Goods();
                goods.setId(rs.getLong("ids"));
                goods.setGoodsImage(rs.getString("goodsImage"));
                goods.setGoodsMoney(rs.getDouble("goodsMoney"));
                goods.setGoodsNumber(rs.getLong("goodsNumber"));
                goods.setGoodsName(rs.getString("goodsName"));
                goods.setGoodsCarriage(rs.getDouble("goodsCarriage"));
                all.setGoods(goods);
                Smallclass smallclass = new Smallclass();
                smallclass.setSmallName(rs.getString("smallName"));
                all.setSmallclass(smallclass);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return all;
    }
}
